package com.ag.utils.param;

import javax.servlet.http.HttpServletRequest;

import com.ag.utils.CollectionUtils;
import com.ag.utils.param.verify.DoubleVerifyAndConvertValue;
import com.ag.utils.param.verify.IntVerifyAndConvertValue;
import com.ag.utils.param.verify.LongVerifyAndConvertValue;
import com.ag.utils.param.verify.NumberInvalidVerifyAndConvertValue;
import com.ag.utils.param.verify.StringNotEmptyVerifyAndConvertValue;

/**
 * 
 * title: ParamUtils.java 
 * description
 *
 * @author rplees
 * @email rplees.i.ly@gmail.com
 * @version 1.0  
 * @created Jul 27, 2017 2:10:20 PM
 */
public class ParamUtils {
	
	public static final IGetValue REQUEST_GETVALUE = new RequestGetValue();
	public static final IGetValue REQUEST_HEADER_GETVALUE = new RequestHeaderGetValue();
	public static final IVerifyAndConvertValue STRING_NOT_EMPTY_VERIFY_AND_CONVERT_VALUE = new StringNotEmptyVerifyAndConvertValue();
	public static final IVerifyAndConvertValue INT_VERIFY_AND_CONVERT_VALUE = new IntVerifyAndConvertValue();
	public static final IVerifyAndConvertValue LONG_VERIFY_AND_CONVERT_VALUE = new LongVerifyAndConvertValue();
	public static final IVerifyAndConvertValue DOUBLE_VERIFY_AND_CONVERT_VALUE = new DoubleVerifyAndConvertValue();
	public static final IVerifyAndConvertValue NUMBER_INVALID_VERIFY_AND_CONVERT_VALUE = new NumberInvalidVerifyAndConvertValue();
	
	/**
	 * 获取请求信息
	 * @param paramName 请求的参数
	 * @param viewName  错误时显示的字段
	 * @param defaultValue 默认值(无默认值则穿NULL,这时如果信息为空则会抛出错误信息)
	 * @param request
	 * @param iGetValue 获取信息的接口
	 * @param iVerifyValues 值的验证与转换
	 * @return T
	 */
	
	public static <T> T getValue(String paramName, String viewName, Object defaultValue, HttpServletRequest request, IGetValue iGetValue, IVerifyAndConvertValue... iVerifyValues) {
		Object value = iGetValue.getValue(paramName, request);
		return verify(viewName, value, iVerifyValues);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T verify(String viewName, Object value, IVerifyAndConvertValue... iVerifyValues) {
		if(CollectionUtils.isNotEmpty(iVerifyValues)) {
			for (IVerifyAndConvertValue iVerifyValue : iVerifyValues) {
				value = iVerifyValue.getValue(value, viewName, null);
			}
		}
		
		return (T) value;
	}
}